package com.yanqu.road.server.cmd.cross.tradewar;

import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.servercenter.ServerInfo;
import com.yanqu.road.entity.tradewar.TradeWarWanted;
import com.yanqu.road.logic.pb.TradeWarPb;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.server.cmd.base.AbstractServerCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerListMgr;
import com.yanqu.road.server.manger.activity.TradeWarActivityMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;

import java.text.MessageFormat;


@Cmd(code = Protocol.S_CROSS_TRADE_WAR_WANTED_FINISH, desc = "跨服商战通缉完成")
public class CrossTradeWarWantedFinishCmd extends AbstractServerCommand {
	private TradeWarWanted tradeWarWanted;
	private int trackCount;
	private int addScore;
	private String reward;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		CrossTradeWarProto.CrossTradeWarWantedFinishMsg reqMsg = CrossTradeWarProto.CrossTradeWarWantedFinishMsg.parseFrom(packet.getBytes());
		tradeWarWanted = TradeWarPb.parseTradeWarWanted(reqMsg.getWantedInfo());
		reward = reqMsg.getReward();
		trackCount = reqMsg.getTrackCount();
		addScore = reqMsg.getAddScore();
		return true;
	}

	@Override
	public void execute(YanQuMessage packet) throws Exception {
		String nickName = tradeWarWanted.getWantedBaseInfo().getNickName();
		String serverName = "";
		ServerInfo serverInfo = ServerListMgr.getServerInfo(tradeWarWanted.getWantedBaseInfo().getServerId());
		if(null != serverInfo){
			serverName = serverInfo.getServerName();
		}
		String title = MultipleLanguageMgr.getContent(MailManager.CROSS_TRADE_WAR_WANTED_FINISH_TITLE, UserMgr.getLanguage(tradeWarWanted.getUserId()));
		String content = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.CROSS_TRADE_WAR_WANTED_FINISH_CONTENT, UserMgr.getLanguage(tradeWarWanted.getUserId())),
				nickName, trackCount, addScore, serverName);
		MailManager.sendMail(tradeWarWanted.getUserId(), eMailType.DAILY.getValue(), reward, content, title);
		//积分变更
		TradeWarActivityMgr.changeCrossTradeWarScore(tradeWarWanted.getUserId(), addScore);
	}

	@Override
	public void sendData(YanQuMessage packet) throws Exception {

	}

}
